//
//  LeftMenuView.m
//  Doctor
//
//  Created by 阳光 on 16/9/26.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "LeftMenuView.h"
#import "CustomBtn.h"

#import <UIImageView+WebCache.h>


@interface LeftMenuView ()

@property (nonatomic,strong)UIButton *selectedBtn;

@end



@implementation LeftMenuView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
      
      [self setMainView];
        
    self.backgroundColor=WhiteColor;
        
    }
    return self;
}

#define customBtnH   55

-(void)setMainView
{
    // 医生头像以及名字点击跳转个人信息界面
    CustomBtn *headBtn=[CustomBtn new];
    headBtn.frame=CGRectMake(kmargin,kmargin*3,APPW*2/3-kmargin*2,customBtnH-1);
    headBtn.userInteractionEnabled=YES;
    [headBtn setImage:CGRectMake(kmargin,(customBtnH-1-96/2)/2,96/2,96/2) title:CGRectMake(kmargin*2+96/2,0,APPW*2/3-kmargin*5,customBtnH-1)];
    [headBtn setTitle:[Util getUserDefaultObjectForKey:@"username"] forState:0];
    headBtn.titleLabel.font=Font_Number(14);
    headBtn.tag=6;
    [headBtn setTitleColor:Color000000 forState:0];
    [headBtn setImage:[UIImage imageNamed:@"head"] forState:0];
    
    if (![[Util getUserDefaultObjectForKey:@"img"] isEqualToString:@""]) {
        [headBtn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[Util getUserDefaultObjectForKey:@"img"]]]] forState:0];
        // 高亮状态处理
         [headBtn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[Util getUserDefaultObjectForKey:@"img"]]]] forState:1];
        headBtn.imageView.layer.masksToBounds = YES;
        headBtn.imageView.layer.cornerRadius = 25;
    }
    
    [headBtn addTarget:self action:@selector(customBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:headBtn];
   
    UIView *onlineView=[[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(headBtn.frame)+kmargin,APPW*2/3,50)];
    onlineView.backgroundColor=WhiteColor;
    [self addSubview:onlineView];
    
    UIView *topLine=[UIView new];
    [onlineView addSubview:topLine];
    topLine.backgroundColor=MAIN_BACK_COLOR;
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW*2/3,1));
        make.top.mas_equalTo(0);
    }];
    
    UIView *bottomLine=[UIView new];
    [onlineView addSubview:bottomLine];
    bottomLine.backgroundColor=MAIN_BACK_COLOR;
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(APPW*2/3,1));
        make.bottom.mas_equalTo(0);
    }];
    
    // 在线模式
    UILabel *onelineLabel=[onlineView getLabelWithText:NSLocalizedString(@"Status", nil) font:Font_Number(13) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"在线模式"
    [onlineView addSubview:onelineLabel];
    [onelineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin*2);
        make.centerY.mas_equalTo(onlineView.mas_centerY);
    }];
    
    UISwitch *switch1=[UISwitch new];
    [onlineView addSubview:switch1];
    switch1.on=YES;
    switch1.backgroundColor=WhiteColor;
    [switch1 addTarget:self action:@selector(switchEvent:) forControlEvents:UIControlEventTouchUpInside];
    [switch1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-kmargin);
        make.centerY.mas_equalTo(onlineView.mas_centerY);
    }];
    
    
    NSArray *iconArr=@[@"home",@"time",@"patient",@"paper",@"guide",@"set"];
//    NSArray *titleArr=@[@"首页",@"设置视频预约时间",@"病人管理",@"论文管理",@"使用手册",@"设置"];
     NSArray *titleArr=@[NSLocalizedString(@"Home", nil) ,NSLocalizedString(@"Video Appintment", nil),NSLocalizedString(@"My Patients", nil),NSLocalizedString(@"My Articles", nil),NSLocalizedString(@"Operation Manual", nil),NSLocalizedString(@"Settings", nil)];
    

    for (int i=0;i<titleArr.count;i++)
    {
        CustomBtn *customBtn=[CustomBtn new];
        [customBtn setImage:CGRectMake(kmargin,(customBtnH-1-kmargin*2)/2,kmargin*2,kmargin*2) title:CGRectMake(kmargin*4,0,APPW*2/3-kmargin*5,customBtnH-1)];
        [customBtn setTitle:titleArr[i] forState:0];
        customBtn.titleLabel.font=Font_Number(14);
        [customBtn setTitleColor:Color000000 forState:0];
        [customBtn setImage:[UIImage imageNamed:iconArr[i]] forState:0];
        [customBtn addTarget:self action:@selector(customBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:customBtn];
        
        customBtn.frame=CGRectMake(kmargin,CGRectGetMaxY(onlineView.frame)+(i%6)*customBtnH,APPW*2/3-kmargin*2,customBtnH-1);
        customBtn.userInteractionEnabled=YES;
        customBtn.tag=i;
        UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(onlineView.frame)+(customBtnH-1)+(i%6)*customBtnH,APPW*2/3,1)];
        lineView.backgroundColor=MAIN_BACK_COLOR;
        [self addSubview:lineView];
        if (i==0) {
            customBtn.selected=YES;
        }
    }
}

// 在线模式开始和关闭
-(void)switchEvent:(UISwitch *)sender
{
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"type":@(sender.on),
                        @"auth_status":@(2)
                        };

    [QBRequest OnlineOrOutlineSwitchWithDic:dic Success:^(id res){
        
        //NSLog(@"message :%@",  res[@"message"]);
        NSString *message = res[@"message"];
        if ([message isEqualToString:@"在线状态开启"]) {//.
             [self makeToast:NSLocalizedString(@"On-Line", nil) duration:.1 position:CSToastPositionCenter];
        } else if ([message isEqualToString:@"在线状态关闭"]){
             [self makeToast:NSLocalizedString(@"Off-Line", nil) duration:.1 position:CSToastPositionCenter];
        } else{
            [self makeToast:res[@"message"] duration:.1 position:CSToastPositionCenter];
        }
       

    } failure:^(NSString *error)
     {
        
    }];
    
}


-(void)setDelegate:(id<LeftMenuViewDelegate>)delegate
{
    _delegate=delegate;
    CustomBtn *btn=(CustomBtn *)[self viewWithTag:0];
    [self customBtnEvent:btn];
    
}

-(void)customBtnEvent:(CustomBtn *)sender
{
    if ([self.delegate respondsToSelector:@selector(leftMenuViewButtonClcikFrom:to:)]) {
        
        [self.delegate leftMenuViewButtonClcikFrom:(LeftMenuViewType)self.selectedBtn.tag to:(LeftMenuViewType)sender.tag];
       // NSLog(@"---%ld --%ld",(long)sender.tag,(long)self.selectedBtn.tag);

    }
}


@end
